/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * larguraDeBanda.java
 *
 * Created on 26/10/2011, 23:12:01
 */
package br.com.fatecpg.tp1.formularios;

import javax.swing.JOptionPane;

/**
 *
 * @author Gledson
 */
public class larguraDeBanda extends javax.swing.JInternalFrame {
    

    public double getConversor(double valor) {

        if (cbNome.getSelectedItem().equals("Selecione")) {
            JOptionPane.showMessageDialog(null, "Por favor selecione uma unidade");
            valor = 0;
            return valor;
        } else if (cbNome.getSelectedItem().equals("Bit/s")) {
            return valor;
        } else if (cbNome.getSelectedItem().equals("Kbit/s")) {
            valor /= 1000;
        } else if (cbNome.getSelectedItem().equals("Mbit/s")) {
            valor /= 1000000;
        } else if (cbNome.getSelectedItem().equals("Gbit/s")) {
            valor *= Math.pow(10, -9);
        } else if (cbNome.getSelectedItem().equals("Tbit/s")) {
            valor *= Math.pow(10, -12);
        } else if (cbNome.getSelectedItem().equals("Byte/s")) {
            valor /= 8;
        } else if (cbNome.getSelectedItem().equals("KB/s")) {
            valor *= Math.pow(10, -3);
        } else {
            valor /= 8000000;
        }
        return valor;
    }


    /** Creates new form larguraDeBanda */
    public larguraDeBanda() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbValor = new javax.swing.JLabel();
        tfValor = new javax.swing.JTextField();
        cbNome = new javax.swing.JComboBox();
        btCalcular = new javax.swing.JButton();
        tfRseultado = new javax.swing.JTextField();
        lbBit = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        labelTitulo = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(63, 98, 104));
        setClosable(true);
        setIconifiable(true);
        setTitle("Conversor de Largura de Banda - (Gledson/Willian)");

        lbValor.setText("Converter Bit/s:");

        cbNome.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione", "Bit/s", "Kbit/s", "Mbit/s", "Gbit/s", "Tbit/s", "Byte/s", "KB/s", "MB/s" }));

        btCalcular.setText("Calcular");
        btCalcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCalcularActionPerformed(evt);
            }
        });

        lbBit.setText("Resultado");

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/fatecpg/tp1/imagens/rodape.png"))); // NOI18N

        labelTitulo.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        labelTitulo.setText("CONVERSOR DE LARGURA DE BANDA");

        jLabel2.setText("Para:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbValor)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tfValor, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btCalcular, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cbNome, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lbBit)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfRseultado, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(85, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addComponent(labelTitulo)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbValor)
                    .addComponent(tfValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(cbNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbBit)
                    .addComponent(tfRseultado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btCalcular, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(jLabel1))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCalcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCalcularActionPerformed
        
        double valor = 0;
        try {
              valor = Double.valueOf(tfValor.getText()).doubleValue();

        } catch (NumberFormatException erro) {
            JOptionPane.showMessageDialog(null, "Por favor insira um valor válido!!");
        }
        tfRseultado.setText(String.valueOf(getConversor(valor)));
        
}//GEN-LAST:event_btCalcularActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCalcular;
    private javax.swing.JComboBox cbNome;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel labelTitulo;
    private javax.swing.JLabel lbBit;
    private javax.swing.JLabel lbValor;
    private javax.swing.JTextField tfRseultado;
    private javax.swing.JTextField tfValor;
    // End of variables declaration//GEN-END:variables
}
